import * as vscode from "vscode";

// 打开文件对话框
const showOpenDialog = (options: vscode.OpenDialogOptions): Promise<vscode.Uri[]> => {
  return new Promise((resolve, reject) => {
    vscode.window.showOpenDialog(options).then((res: vscode.Uri[] | undefined) => {
      if (res === undefined) reject();
      resolve(<vscode.Uri[]>res);
    })
  })
}

// 新建一个活动窗体
const newUntitledFile = (): Promise<vscode.TextEditor | void> => {
  return new Promise((resolve, reject) => {
    vscode.commands.executeCommand("workbench.action.files.newUntitledFile").then(() => {
      // 获取当前的编辑器对象
      const textEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
      if (textEditor) {
        resolve(textEditor);
      } else {
        reject();
      }
    });
  });
}

// 弹出input
const showInputBox = (options: vscode.InputBoxOptions): Promise<string | void> => {
  return new Promise((resolve, reject) => {
    vscode.window.showInputBox(options).then((value: string | undefined) => {
      if (value?.trim()) {
        resolve(value);
      } else {
        reject();
      }
    })
  });
};

// 创建状态栏对象
const createStatus = (option: statusOption): vscode.StatusBarItem => {
  const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 9999);
  for (const [key, value] of Object.entries(option)) {
    statusBarItem[key as keyof statusOption] = value;
  }

  // statusBarItem.name = "myStatusInfo";
  // statusBarItem.text = "$(sync~spin)loading...";
  // statusBarItem.tooltip = "正在翻译中";
  // statusBarItem.show();
  return statusBarItem;
}

// 获取配置
const getConfiguration = <T>(key: string): T | void => {
  return vscode.workspace.getConfiguration().get(key);
}

// 设置配置
const setConfiguration = <T>(key: string, value: T, flog: boolean): void => {
  vscode.workspace.getConfiguration().update(key, value, flog);
}


export {
  showOpenDialog,
  newUntitledFile,
  showInputBox,
  getConfiguration,
  setConfiguration,
  createStatus
}